package com.quyeying.controller;

import com.quyeying.utils.JwtUtil;
import com.quyeying.utils.SmsUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.TblMerchantService;
import com.quyeying.entity.TblMerchantEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* tbl_merchant 前端控制器
* </p>
* @author skx
* @since 2022-12-12
*/
@Slf4j
@RestController
@RequestMapping("merchantEntity")
public class TblMerchantController {

    @Resource
    private TblMerchantService tblMerchantService;
    @Resource
    SmsUtil smsUtil;
    @Value("${sms-code}")
    String smsCode;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody TblMerchantEntity tblMerchantEntity){
        try {
            //如果输入跳过的验证码不用验证
            if (!smsCode.equals(tblMerchantEntity.getVerificationCode())){
                if(!smsUtil.checkSmsCode(tblMerchantEntity.getAccount(), tblMerchantEntity.getVerificationCode())) {
                    return R.error(ResultEnum.VERIFYCODE_INVALID);
                }
            }
            return tblMerchantService.add(tblMerchantEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblMerchantService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblMerchantEntity tblMerchantEntity){
        try {
            return tblMerchantService.updateData(tblMerchantEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
    @RequestParam Integer size){
        try {
            return tblMerchantService.findListByPage(current, size);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 获取商户信息
    **/
    @GetMapping("/geyMerchantInfo")
    public R geyMerchantInfo(HttpServletRequest request){
        try {
            String account = JwtUtil.getUserName(request.getHeader("Authorization"));
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(account);
            return R.success(merchantInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
